<template>
  <div>
    <!-- 手机 -->
    <div class="content">
      <h3 class="user-title">{{'CHANGE_EMAIL'|t}}</h3>
      <div v-show="defaultIndex==1">
        <!-- 通过手机验证 -->
        <identity-through-mobile @nextStep='nextStep(2)' v-show="queryType=='mobile'"></identity-through-mobile>
        <!-- 通过邮箱验证 -->
        <identity-through-email @nextStep='nextStep(2)' v-show="queryType=='email'"></identity-through-email>
      </div>
      <!-- 邮箱 -->
      <div class="form" v-show="defaultIndex==2">
        <zx-form-group :title="$t('NEW_EMAIL')">
          <zx-input v-model="newEmail.mail" :place="$t('NEW_EMAIL_REQUIRE')" ref="mail" :rules="rules.email">
            <span class="input-text" slot="text" @click="getCode(newEmail.mail,'mail',$t('GET_CODE'),$event,'newEmailBool')">{{'GET_CODE'|t}}</span>
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('CODE_LABEL')">
          <zx-input v-model="newEmail.codes" :place="$t('CODE_PLACE')" ref="codes" :rules="rules.code">
          </zx-input>
        </zx-form-group>
        <zx-form-group>
          <input type="button" class="submit-btn mt-20 green-btn radius" :value="$t('COMFIRM')" @click="submitForm('newEmail')">
        </zx-form-group>
      </div>
    </div>
  </div>
</template>
<script>
  import { mapGetters } from 'vuex';
  import { Form, Func, Auth, Rules } from '@/providers';
  import { User, UserInfo } from '@/services';
  import identityThroughMobile from '@/components/identity-through-mobile';
  import identityThroughEmail from '@/components/identity-through-email';
  export default {
    data() {
      return {
        newEmailBool: false,
        queryType: 'mobile',
        defaultIndex: 2,
        newEmail: {
          mail: '',
          codes: ''
        },
        rules: Rules
      };
    },
    methods: {
      // 获取验证码
      getCode(val, type, innerText, event, clickBool) {
        if (this[clickBool]) { return false; };
        if (!Form.validItem(this, type)) { return; };
        this[clickBool] = true;
        if (type == 'phone') {
          Form.SendMobile(val, event.target, clickBool, innerText);
        } else {
          Form.SentMail(val, event.target, clickBool, innerText);
        }
      },
      nextStep(step) {
        this.defaultIndex = step;
      },
      async submitForm(formName, step) {
        if (!Form.validataForm(this, formName)) {
          return false;
        }
        let _parmas = this[formName];
        try {
          let res = await User.ModifyMail({
            token: Auth.getItem('token'),
            code: _parmas.codes,
            mail: _parmas.mail
          });
          if (!Form.checkResToken(res)) {
            return;
          }
          this.$dialog.toast({
            position: 'top',
            msg: res.desc
          });
        } catch (error) {
          console.log(error);
        }
      }
    },
    components: {
      'identity-through-mobile': identityThroughMobile,
      'identity-through-email': identityThroughEmail
    },
    created() {
      this.queryType = this.$route.query.type;
    }
  };
</script>
<style scoped lang="scss">
  @import "../../assets/sass/user.scss";
</style>